package com.realtyManage.springboot.Controller;

import com.alibaba.fastjson.JSON;
import com.realtyManage.springboot.Model.Do.*;
import com.realtyManage.springboot.Model.Vo.BaseEntity;
import com.realtyManage.springboot.Model.Vo.CustomReEntity;
import com.realtyManage.springboot.Model.Vo.RegEntity;
import com.realtyManage.springboot.Service.*;
import com.realtyManage.springboot.Service.impl.CustomerRegistrationServiceImpl;
import com.realtyManage.springboot.Service.impl.HouseDetailServiceImpl;
import com.realtyManage.springboot.Utils.AuthenticationAuthorityUtil;
import com.realtyManage.springboot.Utils.BuildTreeUtil;
import com.realtyManage.springboot.Utils.SysExecRsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class ProjectDetailController {

    @Autowired
    private ProjectDetailService projectDetailService;

    @Autowired
    private IUserService userService;

    @Autowired
    private CustomerRegistrationServiceImpl customerRegistrationServiceImpl;

    @Autowired
    private HouseDetailServiceImpl houseDetailServiceImpl;

    @Autowired
    private IQuestionsAnswersService questionsAnswersService;

    @Autowired
    private IContactService contactService;

    @Autowired
    private ICustomerRegistrationService customerRegistrationService;

    @Autowired
    private IApplyInfoService applyInfoService;

    /**
     * 查询楼盘最新动态5条
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showNewHouseDynamic",method = RequestMethod.GET)
    private String showNewHouseDynamic(HttpServletRequest request) {
        List<HouseDynamic> areaList = projectDetailService.showNewHouseDynamic();
        return JSON.toJSONString(areaList);
    }


    /**
     * 查询楼盘动态列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showHouseDynamic",method = RequestMethod.GET)
    private String showHouseDynamic(HttpServletRequest request) {
        List<HouseDynamic> areaList = projectDetailService.showHouseDynamic();
        return JSON.toJSONString(areaList);
    }


    /**
     * 物理删除楼盘动态信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteHouseDynamic",method = RequestMethod.POST)
    @ResponseBody
    private String deleteHouseDynamic(String houseDynamicId, HttpServletRequest request) {
        Integer code = -1;
        List ids = new ArrayList();
        String dynamicId[] = houseDynamicId.split(",");
        for (String id : dynamicId){
            ids.add(id);
        }
        try{
            if (ids.size() > 0){
                if (projectDetailService.deleteHouseDynamic(ids) > 0){
                    code = 1;
                }else{
                    code = -1;
                }
            }
        }catch (Exception e){
            e.getMessage();
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 新增楼盘动态
     * @param houseDynamic
     * @param request
     * @return
     */
    @RequestMapping(value = "/addHouseDynamic",method = RequestMethod.POST)
    @ResponseBody
    private String addHouseDynamic(HouseDynamic houseDynamic, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        houseDynamic.setInsertTime(sdf.format(new Date()));
        houseDynamic.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        houseDynamic.setUserNam(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getName());
        if (projectDetailService.addHouseDynamic(houseDynamic).getId() != null){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 修改楼盘动态
     * @param houseDynamic
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateHouseDynamic",method = RequestMethod.POST)
    @ResponseBody
    private String updateHouseDynamic(HouseDynamic houseDynamic, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        houseDynamic.setInsertTime(sdf.format(new Date()));
        houseDynamic.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        houseDynamic.setUserNam(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getName());
        if (projectDetailService.updateHouseDynamic(houseDynamic) > 0){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 新增或修改项目信息
     * @param projectDetail
     * @param request
     * @return
     */
    @RequestMapping(value = "/addOrUpdateProjectDetail",method = RequestMethod.POST)
    @ResponseBody
    private String addProjectDetail(ProjectDetail projectDetail, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        projectDetail.setInsertTime(sdf.format(new Date()));
        projectDetail.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        projectDetail.setUserName(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getName());
        if (projectDetailService.addOrUpdateProjectDetail(projectDetail).getId() != null){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 新增周边配套信息
     * @param projectAssort
     * @param request
     * @return
     */
    @RequestMapping(value = "/addProjectAssort",method = RequestMethod.POST)
    @ResponseBody
    private String addProjectAssort(ProjectAssort projectAssort, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        projectAssort.setInsertTime(sdf.format(new Date()));
        projectAssort.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        projectAssort.setUserName(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getName());
        if (projectDetailService.addProjectAssort(projectAssort).getId() != null){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /***
     * 删除周边配套信息
     * @param projectAssortId
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteProjectAssort",method = RequestMethod.POST)
    @ResponseBody
    private String deleteProjectAssort(String projectAssortId, HttpServletRequest request) {
        Integer code = -1;
        List ids = new ArrayList();
        String dynamicId[] = projectAssortId.split(",");
        for (String id : dynamicId){
            ids.add(id);
        }
        try{
            if (ids.size() > 0){
                if (projectDetailService.deleteProjectAssort(ids) > 0){
                    code = 1;
                }else{
                    code = -1;
                }
            }
        }catch (Exception e){
            e.getMessage();
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 查询配套
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryPt",method = RequestMethod.GET)
    private String queryPt(HttpServletRequest request) {
        String type = request.getParameter("type");
        List<ProjectAssort> areaList = projectDetailService.queryPt(Integer.valueOf(type));
        return JSON.toJSONString(areaList);
    }


    /**
     * 项目展示
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showProjectExhibition",method = RequestMethod.GET)
    private String showProjectExhibition(HttpServletRequest request) {
        List<ProjectExhibition> areaList = projectDetailService.showProjectExhibition();
        return JSON.toJSONString(areaList);
    }


    /**
     * 获取项目详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showProject",method = RequestMethod.GET)
    private String showProject(HttpServletRequest request) {
        List<ProjectDetail> areaList = projectDetailService.showProject();
        return JSON.toJSONString(areaList);
    }


    /**
     * 修改展示信息
     * @param projectExhibition
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateProjectExhibition",method = RequestMethod.POST)
    @ResponseBody
    private String updateProjectExhibition(ProjectExhibition projectExhibition, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        projectExhibition.setUpdateTime(sdf.format(new Date()));
        projectExhibition.setUpdateUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString());
        if (projectDetailService.updateProjectExhibition(projectExhibition) > 0){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 新增项目户型
     * @param projectApartment
     * @param request
     * @return
     */
    @RequestMapping(value = "/addProjectApartment",method = RequestMethod.POST)
    @ResponseBody
    private String addProjectApartment(ProjectApartment projectApartment, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        projectApartment.setInsertTime(sdf.format(new Date()));
        if (projectDetailService.addProjectApartment(projectApartment).getId() != null){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 删除项目户型
     * @param projectApartmentId
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteProjectApartment",method = RequestMethod.POST)
    @ResponseBody
    private String deleteProjectApartment(String projectApartmentId, HttpServletRequest request) {
        Integer code = -1;
        List ids = new ArrayList();
        String dynamicId[] = projectApartmentId.split(",");
        for (String id : dynamicId){
            ids.add(id);
        }
        try{
            if (ids.size() > 0){
                if (projectDetailService.deleteProjectApartment(ids) > 0){
                    code = 1;
                }else{
                    code = -1;
                }
            }
        }catch (Exception e){
            e.getMessage();
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 修改项目户型
     * @param projectApartment
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateProjectApartment",method = RequestMethod.POST)
    @ResponseBody
    private String updateProjectApartment(ProjectApartment projectApartment, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        if (projectDetailService.updateProjectApartment(projectApartment) > 0){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    //统计室
    @ResponseBody
    @RequestMapping(value = "/getCountHouseScale",method = RequestMethod.GET)
    private String getCountHouseScale(HttpServletRequest request) {
        List areaList = projectDetailService.getCountHouseScale();
        return JSON.toJSONString(areaList);
    }

    //统计物业类型
    @ResponseBody
    @RequestMapping(value = "/getCountPropertyType",method = RequestMethod.GET)
    private String getCountPropertyType(HttpServletRequest request) {
        Map areaList = projectDetailService.getCountPropertyType();
        return JSON.toJSONString(areaList);
    }

    /**
     * 户型编号下拉
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getApartment",method = RequestMethod.GET)
    private String getApartmentNumber(HttpServletRequest request) {
        List apartmentList = projectDetailService.getApartment();
        return JSON.toJSONString(apartmentList);
    }


    /**
     * 获取编码信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getApartmentNumberList",method = RequestMethod.GET)
    private String getApartmentNumberList(HttpServletRequest request) {
        String code = request.getParameter("code");
        List getApartmentNumberList = projectDetailService.getApartmentNumberList(code);
        return JSON.toJSONString(getApartmentNumberList);
    }

    /**
     * 获取物业类型房屋信息
     * @param projectApartment
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPropertyTyeList",method = RequestMethod.GET)
    private String getPropertyTyeList(ProjectApartment projectApartment,HttpServletRequest request) {
        List<ProjectApartment> rest = projectDetailService.getPropertyTyeList(projectApartment);
        return JSON.toJSONString(rest);
    }

    /**
     * 查询项目户型
     * @param projectApartment
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProjectApartmentList",method = RequestMethod.GET)
    private String getProjectApartmentList(ProjectApartment projectApartment,HttpServletRequest request) {
        List<ProjectApartment> rest = projectDetailService.findPagedByWhereClause(projectApartment);
        return JSON.toJSONString(rest);
    }


    /**
     * 修改积分信息
     * @param integral
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateIntegral",method = RequestMethod.POST)
    @ResponseBody
    private String updateIntegral(Integral integral, HttpServletRequest request) {
        Integer code = -1;
        if (projectDetailService.updateIntegral(integral) > 0){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 查询积分信息列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getIntegralList",method = RequestMethod.GET)
    private String getIntegralList(HttpServletRequest request) {
        List<Integral> areaList = projectDetailService.getIntegralList();
        return JSON.toJSONString(areaList);
    }

    /**
     * 查询客户公池统计
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCountPublicCustomer",method = RequestMethod.GET)
    private String getCountPublicCustomer(HttpServletRequest request) {
        List areaList = projectDetailService.getCountPublicCustomer();
        return JSON.toJSONString(areaList);
    }


    /**
     * 本月任务人员列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCompleteTaskSalespersonList",method = RequestMethod.GET)
    private String getCompleteTaskSalespersonList(HttpServletRequest request) {
        String complete = request.getParameter("complete");
        List<User> areaList = projectDetailService.getCompleteTaskSalespersonList(complete);
        return JSON.toJSONString(areaList);
    }

    /**
     * 本月置业顾问任务完成情况
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCompleteTask",method = RequestMethod.GET)
    private String getCompleteTask(HttpServletRequest request) {
        List<Integral> areaList = projectDetailService.getIntegralList();
        return JSON.toJSONString(areaList);
    }

    /**
     * 查询未完成的非标任务列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInformal",method = RequestMethod.GET)
    private String getInformal(HttpServletRequest request) {
        List<UserTask> areaList = projectDetailService.getInformal();
        return JSON.toJSONString(areaList);
    }


    /**
     * 获取任务详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryTaskDetailById",method = RequestMethod.GET)
    private String queryTaskDetailById(HttpServletRequest request) {
        String id = request.getParameter("id");
        UserTask ut = projectDetailService.queryTaskDetailById(Integer.valueOf(id));
        return JSON.toJSONString(ut);
    }


    /**
     * 统计项目图片数量
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCountImg",method = RequestMethod.GET)
    private String getCountImg(HttpServletRequest request) {
        List imgList = projectDetailService.getCountImg();
        return JSON.toJSONString(imgList);
    }

    /**
     * 查询详图
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getImgByType",method = RequestMethod.GET)
    private String getImgByType(HttpServletRequest request) {
        String type = request.getParameter("type");
        List<ProjectImg> img = projectDetailService.getImgByType(Integer.valueOf(type));
        return JSON.toJSONString(img);
    }


    /**
     * 修改项目图片
     * @param projectImg
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateProjectImgByType",method = RequestMethod.POST)
    @ResponseBody
    private String updateProjectImgByType(ProjectImg projectImg, HttpServletRequest request) {
        Integer code = -1;
        if (projectDetailService.updateProjectImgByType(projectImg) > 0){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 获取你问我答
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDiscussTree",method = RequestMethod.GET)
    private String getDiscussTree(HttpServletRequest request){
        List<Discuss> list  = projectDetailService.getDiscussTree();
        List<Discuss> listTree = BuildTreeUtil.buildTlTree(list,0);
        return  JSON.toJSONString(listTree);
    }


    /**
     * 获取问答详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDiscussTreeDetailById",method = RequestMethod.GET)
    private String getDiscussTreeDetailById(HttpServletRequest request){
        String id = request.getParameter("id");
        List<Discuss> list  = projectDetailService.getDiscussTreeDetailById(id);
        List<Discuss> listTree = BuildTreeUtil.buildTlTree(list,0);
        return  JSON.toJSONString(listTree);
    }


    /**
     * 删除你问我答信息
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteDiscuss",method = RequestMethod.POST)
    @ResponseBody
    private String deleteDiscuss(String id, HttpServletRequest request) {
        Integer code = -1;
        if (projectDetailService.deleteDiscuss(Integer.valueOf(id)) > 0){
            code = 1;
        }else{
            code = -1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 获取待审核问答
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAuditDiscuss",method = RequestMethod.GET)
    private String getAuditDiscuss(HttpServletRequest request) {
        List<Discuss> lst = projectDetailService.getAuditDiscuss();
        return JSON.toJSONString(lst);
    }


    /**
     * 审核问答
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/auditDiscuss",method = RequestMethod.POST)
    @ResponseBody
    private String auditDiscuss(String id,String type, HttpServletRequest request) {
        Integer code = -1;
        if (projectDetailService.auditDiscuss(Integer.valueOf(id),type) > 0){
            code = 1;
        }else{
            code = -1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 新增你问我答
     * @param discuss
     * @param request
     * @return
     */
    @RequestMapping(value = "/addDiscuss",method = RequestMethod.POST)
    @ResponseBody
    private String addDiscuss(Discuss discuss, HttpServletRequest request) {
        RegEntity regInfo = new RegEntity();
       // String qx = request.getParameter("qx");
       // String userId = request.getParameter("userId");
       // Integer value = AuthenticationAuthorityUtil.Verification(userId,qx);
        /*if (value == -1){
            regInfo.setInfoCode(-6000);
            regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(-6000));
            return JSON.toJSONString(regInfo);
        }*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        discuss.setTime(sdf.format(new Date()));
        if (discuss.getParentId() == null){
            discuss.setParentId("0");
            discuss.setType(0);
        }else{
            discuss.setType(1);
        }
        if (projectDetailService.addDiscuss(discuss).getId() != null){
            code = 1;
        }
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 查询客户详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryCustomerDetail",method = RequestMethod.GET)
    private String queryCustomerDetail(HttpServletRequest request) {
        String customerId = request.getParameter("customerId");
        List<CustomerRegistration> img = customerRegistrationServiceImpl.queryCustomerDetail(customerId);
        return JSON.toJSONString(img);
    }


    /**
     * 查询置业名称和图片
     * @param request
     * @param customReEntity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getNameAndImg",method = RequestMethod.GET)
    private String getNameAndImg(HttpServletRequest request, CustomReEntity customReEntity) {
       // customReEntity.setName(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getName());
        List<String> lst = projectDetailService.getNameAndImg(customReEntity);
        return JSON.toJSONString(lst);
    }


    /**
     * 查询房源信息详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getHouseDetailById",method = RequestMethod.GET)
    private String getHouseDetailById(HttpServletRequest request) {
        String id = request.getParameter("houseId");
        List<HouseDetail> lst = projectDetailService.getHouseDetailById(id);
        return JSON.toJSONString(lst);
    }

    /**
     * 新增置业计划书
     * @param homePurchasePlan
     * @param request
     * @return
     */
    @RequestMapping(value = "/addHomePurchasePlan",method = RequestMethod.POST)
    @ResponseBody
    private String addHomePurchasePlan(HomePurchasePlan homePurchasePlan, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        Integer code = -1;
        Integer id = 0;
        homePurchasePlan.setTime(sdf.format(new Date()));
        id = projectDetailService.addHomePurchasePlan(homePurchasePlan).getId();
        if (id != null){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setId(id);
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 删除置业计划书
     * @param homePurchasePlan
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteHomePurchasePlan",method = RequestMethod.POST)
    @ResponseBody
    private String deleteHomePurchasePlan(HomePurchasePlan homePurchasePlan, HttpServletRequest request) {
        Integer code = -1;
        if (projectDetailService.deleteHomePurchasePlan(homePurchasePlan.getId()) > 0){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 查询我的置业计划书
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getHomePurchasePlan",method = RequestMethod.GET)
    private String getHomePurchasePlan(HttpServletRequest request) {
        String id = request.getParameter("userId");
        List<HomePurchasePlan> lst = projectDetailService.getHomePurchasePlan(id);
        return JSON.toJSONString(lst);
    }


    /**
     * 获取置业计划书详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getHomePurchasePlanDetail",method = RequestMethod.GET)
    private String getHomePurchasePlanDetail(HttpServletRequest request) {
        String id = request.getParameter("id");
        HomePurchasePlan lst = projectDetailService.getHomePurchasePlanDetail(id);
        return JSON.toJSONString(lst);
    }

    /**
     * 获取置业顾问姓名及号码
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getNameAndPhone",method = RequestMethod.GET)
    private String getNameAndPhone(HttpServletRequest request) {
        List lst = projectDetailService.getNameAndPhone();
        return JSON.toJSONString(lst);
    }

    /**
     * 获取用户id
     * @param request
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserId",method = RequestMethod.GET)
    private int getUserId(HttpServletRequest request,String phone) {
        Integer id = userService.findByTelePhone(phone).getId();
        return id;
    }

    /**
     * 下拉幢号
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryBlockCode",method = RequestMethod.GET)
    private String queryBlockCode(HttpServletRequest request) {
        return JSON.toJSONString(houseDetailServiceImpl.queryBlockCode());
    }

    /**
     * 物业类型
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryPropertyType",method = RequestMethod.GET)
    private String queryPropertyType(HttpServletRequest request) {
        return JSON.toJSONString(houseDetailServiceImpl.queryPropertyType());
    }

    /**
     * 获取期数
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryStage",method = RequestMethod.GET)
    private String queryStage(HttpServletRequest request) {
        String propertyType = request.getParameter("propertyType"); //物业类型
        return JSON.toJSONString(houseDetailServiceImpl.queryStage(propertyType));
    }

    /**
     * 获取栋号
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryTung",method = RequestMethod.GET)
    private String queryTung(HttpServletRequest request) {
        String propertyType = request.getParameter("propertyType"); //物业类型
        String stage = request.getParameter("stage"); //期数
        return JSON.toJSONString(houseDetailServiceImpl.queryTung(propertyType,stage));
    }

    /**
     * 单元号下拉
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryUnitCode",method = RequestMethod.GET)
    private String queryUnitCode(HttpServletRequest request) {
        String blockCode = request.getParameter("blockCode"); //幢号
        return JSON.toJSONString(houseDetailServiceImpl.queryUnitCode(blockCode));
    }

    /**
     * 根据单元号下拉门牌号
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDoorplateCode",method = RequestMethod.GET)
    private String queryDoorplateCode(HttpServletRequest request) {
        String blockCode = request.getParameter("blockCode"); //幢号
        String unitCode = request.getParameter("unitCode"); //单元号
        return JSON.toJSONString(houseDetailServiceImpl.queryDoorplateCode(blockCode,unitCode));
    }


    /**
     * 根据幢号下拉门牌号
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDoorplateCodeZ",method = RequestMethod.GET)
    private String queryDoorplateCodeS(HttpServletRequest request) {
        String blockCode = request.getParameter("blockCode"); //幢号
        return JSON.toJSONString(houseDetailServiceImpl.queryDoorplateCodeZ(blockCode));
    }




    /**
     * 根据房源幢，单元，室查询信息
     * @param request
     * @param houseDetail
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryHouseDetail",method = RequestMethod.GET)
    private String queryHouseDetail(HttpServletRequest request,HouseDetail houseDetail) {
        return JSON.toJSONString(houseDetailServiceImpl.queryHouseDetail(houseDetail));
    }

    /**
     * 客户销售记信息选择继续跟踪时，此销售成交信息清空
     * @param request
     * @return
     */
    @RequestMapping(value = "/emptyRecord",method = RequestMethod.POST)
    @ResponseBody
    private String emptyRecord(CustomerRegistration customerRegistration,HttpServletRequest request) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        Integer xr = 0; // 1为有房源信息  0默认取无
        RegEntity regInfo = new RegEntity();
            //存在小订及认筹会有房源信息
            if (customerRegistration.getBlockCode() != null && customerRegistration.getDoorplateCode() != null){
                HouseDetail houseDetail = houseDetailServiceImpl.getBlockCodeY(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode());
                if (houseDetail !=  null){
                    if (houseDetail.getArtificial().equals("1") || houseDetail.getArtificial().equals("3")){   //销控为已定,已售的不可再进行操作
                        code = -10086;
                        regInfo.setInfoCode(code);
                        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                        return JSON.toJSONString(regInfo);
                    }
                    if (houseDetail.getRelatedAdviser() != null && houseDetail.getRelatedAdviser() != 0){
                        Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
                        System.out.println("===========>"+uid+"===================>"+houseDetail.getRelatedAdviser());
                        if (!customerRegistration.getId().equals(houseDetail.getRelatedAdviser()) ){
                            code = -10086;
                            regInfo.setInfoCode(code);
                            regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                            return JSON.toJSONString(regInfo);
                        }
                        if (!houseDetail.getRelatedAdviser().toString().equals(uid.toString())){  //不是同一用户不能进行房源操作
                                code = -10086;
                                regInfo.setInfoCode(code);
                                regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                                return JSON.toJSONString(regInfo);
                        }
                    }
                }
            }
        if (customerRegistration.getCustomerType().equals("小订")){  //小订状态和小订金额入库
            customerRegistration.setSmallSum(1);
            customerRegistration.setSmallSumMoney(customerRegistration.getReturnedMoney()); //金额（已回款）
            if (customerRegistrationServiceImpl.updateCustomerType(customerRegistration) > 0){
                customerRegistrationServiceImpl.updateXd(customerRegistration.getId());
                HouseDetail hd = new HouseDetail();
                hd.setRelatedParty(customerRegistration.getId());
                hd.setRelatedAdviser(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
                hd.setTotalActualAmount(customerRegistration.getTotalActualAmount());
                hd.setCjTime(df.format(new Date()));
                hd.setType("2");
                hd.setArtificial(1);
                //查询是否有购房记录
                int value = houseDetailServiceImpl.countNum(hd.getRelatedParty());
                if (value > 0){
                    //判断是否为同一房源
                    HouseDetail h = houseDetailServiceImpl.countHouse(customerRegistration.getId());
                    Integer t = 0;
                    if (customerRegistration.getBlockCode().toString() != null){
                        if (h.getBlockCode() != customerRegistration.getBlockCode()){
                            t = 1;
                        }
                    }
                    if (customerRegistration.getUnitCode() != null){
                        if (!h.getUnitCode().equals(customerRegistration.getUnitCode())){
                            t = 1;
                        }
                    }
                    if (customerRegistration.getDoorplateCode() != null){
                        if (!h.getDoorplateCode().equals( customerRegistration.getDoorplateCode())){
                            t = 1;
                        }
                    }
                    //t为1的时候则是新改变的房源
                    if (t == 1){
                        if (houseDetailServiceImpl.getBlockCodeY(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode()).getId() !=  null){
                            code = -10086;
                            regInfo.setInfoCode(code);
                            regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                            return JSON.toJSONString(regInfo);
                        }
                        //清除过往记录
                        houseDetailServiceImpl.clear(customerRegistration.getId());
                        //查询新房源id
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                        //根据id更新信息
                        if (houseDetailServiceImpl.updateHouseInfo(hd)){
                            code = 1;
                        }
                    }else{
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseInfo(hd)){
                            code = 1;
                        }
                    }
                }else {
                    //插入客户入手房源信息
                    //存在小订及认筹会有房源信息
                    if (customerRegistration.getBlockCode() != null && customerRegistration.getDoorplateCode() != null) {
                        //查询房源在售id
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(), customerRegistration.getUnitCode(), customerRegistration.getDoorplateCode())));
                        //根据id修改房源与客户相关信息
                        if (houseDetailServiceImpl.updateHouseInfo(hd)) {
                            code = 1;
                        }
                    }else{
                        code = 1;
                    }
                }
            }
        }else if (customerRegistration.getCustomerType().equals("大定")){ //大订状态和大订金额入库
            customerRegistration.setLargeSum(1);
            customerRegistration.setLargeSumMoney(customerRegistration.getReturnedMoney());
            customerRegistration.setMoneyBack(customerRegistration.getReturnedMoney());
            if (customerRegistrationServiceImpl.updateCustomerType(customerRegistration) > 0){
                customerRegistrationServiceImpl.updateDd(customerRegistration.getId());
                HouseDetail hd = new HouseDetail();
                hd.setRelatedParty(customerRegistration.getId());
                hd.setRelatedAdviser(customerRegistration.getReceptionId());
                hd.setType("3");
                hd.setRelatedAdviser(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
                hd.setTotalActualAmount(customerRegistration.getTotalActualAmount());
                hd.setCjTime(df.format(new Date()));
                hd.setArtificial(3);
                //查询是否有购房记录
                int value = houseDetailServiceImpl.countNum(hd.getRelatedParty());
                if (value > 0){
                    //判断是否为同一房源
                    HouseDetail h = houseDetailServiceImpl.countHouse(customerRegistration.getId());
                    Integer t = 0;
                    if (customerRegistration.getBlockCode().toString() != null){
                        if (h.getBlockCode() != customerRegistration.getBlockCode()){
                            t = 1;
                        }
                    }
                    if (customerRegistration.getUnitCode() != null){
                        if (!h.getUnitCode().equals(customerRegistration.getUnitCode())){
                            t = 1;
                        }
                    }
                    if (customerRegistration.getDoorplateCode() != null){
                        if (!h.getDoorplateCode().equals( customerRegistration.getDoorplateCode())){
                            t = 1;
                        }
                    }
                    //t为1的时候则是新改变的房源
                    if (t == 1){
                        //新改的房源是否被锁定
                        if (houseDetailServiceImpl.getBlockCodeY(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode()).getId() != null){
                            code = -10086;
                            regInfo.setInfoCode(code);
                            regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                            return JSON.toJSONString(regInfo);
                        }
                        //清除之前操作
                        houseDetailServiceImpl.clear(customerRegistration.getId());
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseType(hd)){
                            code = 1;
                        }
                    }else{
                        //修改最新信息
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseInfo(hd)){
                            code = 1;
                        }
                    }
                }else {
                    //插入用户购房信息
                    hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                    //根据id修改房源与客户相关信息
                    if (houseDetailServiceImpl.updateHouseInfo(hd)){
                        code = 1;
                    }
                }
            }
        }else if (customerRegistration.getCustomerType().equals("已交首付")){ //已交首付金额入库
            customerRegistration.setDownPayments(1);
            customerRegistration.setDownPaymentsMoney(customerRegistration.getReturnedMoney());
            customerRegistration.setMoneyBack(customerRegistration.getReturnedMoney());
            if (customerRegistrationServiceImpl.updateCustomerType(customerRegistration) > 0){
                customerRegistrationServiceImpl.updateSf(customerRegistration.getId());
                HouseDetail hd = new HouseDetail();
                hd.setRelatedParty(customerRegistration.getId());
                hd.setType("4");
                hd.setTotalActualAmount(customerRegistration.getTotalActualAmount());
                hd.setRelatedAdviser(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
                hd.setCjTime(df.format(new Date()));
                hd.setTotalActualAmount(customerRegistration.getTotalActualAmount());
                hd.setArtificial(3);
                //2019-12-25 下午  首付及更新最新的报备状态  如果是报备客户  Chenxinlele
                String customerSource = customerRegistrationServiceImpl.findBb(customerRegistration.getId());
                if (customerSource != null){ //执行佣金状态操作
                    String virtualNumber = customerRegistrationServiceImpl.findvirtualNumber(customerRegistration.getId());
                    applyInfoService.updateStep(virtualNumber);
                }
                //查询是否有购房记录
                int value = houseDetailServiceImpl.countNum(hd.getRelatedParty());
                if (value > 0){
                    //判断是否为同一房源
                    HouseDetail h = houseDetailServiceImpl.countHouse(customerRegistration.getId());
                    Integer t = 0;
                    if (customerRegistration.getBlockCode().toString() != null){
                        if (h.getBlockCode() != customerRegistration.getBlockCode()){
                            t = 1;
                        }
                    }
                    if (customerRegistration.getUnitCode() != null){
                        if (!h.getUnitCode().equals(customerRegistration.getUnitCode())){
                            t = 1;
                        }
                    }
                    if (customerRegistration.getDoorplateCode() != null){
                        if (!h.getDoorplateCode().equals( customerRegistration.getDoorplateCode())){
                            t = 1;
                        }
                    }
                    //t为1的时候则是新改变的房源
                    if (t == 1){
                        //新改的房源是否被锁定
                        if (houseDetailServiceImpl.getBlockCodeY(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode()).getId() != null){
                            code = -10086;
                            regInfo.setInfoCode(code);
                            regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                            return JSON.toJSONString(regInfo);
                        }
                        //清除之前操作
                        houseDetailServiceImpl.clear(customerRegistration.getId());
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseType(hd)){
                            code = 1;
                        }
                    }else{
                        //修改最新信息
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseInfo(hd)){
                            code = 1;
                        }
                    }
                }else{
                    //修改最新信息
                    hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                    if (houseDetailServiceImpl.updateHouseInfo(hd)){
                        code = 1;
                    }
                }
            }
        }else if (customerRegistration.getCustomerType().equals("全款/放款")){ //已全款金额入库
            customerRegistration.setDealPaymentType("全款/放款");
            customerRegistration.setPaymentTypeMoney(customerRegistration.getReturnedMoney());
            customerRegistration.setMoneyBack(customerRegistration.getReturnedMoney());
            if (customerRegistrationServiceImpl.updateCustomerType(customerRegistration) > 0){
                customerRegistrationServiceImpl.updateQk(customerRegistration.getId());
                HouseDetail hd = new HouseDetail();
                hd.setRelatedParty(customerRegistration.getId());
                hd.setTotalActualAmount(customerRegistration.getTotalActualAmount());
                hd.setRelatedAdviser(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
                hd.setTotalActualAmount(customerRegistration.getTotalActualAmount());
                hd.setCjTime(df.format(new Date()));
                hd.setType("4");
                hd.setArtificial(3);
                //2019-12-25 下午  首付及更新最新的报备状态  如果是报备客户  Chenxinlele
                String customerSource = customerRegistrationServiceImpl.findBb(customerRegistration.getId());
                if (customerSource != null){ //执行佣金状态操作
                    String virtualNumber = customerRegistrationServiceImpl.findvirtualNumber(customerRegistration.getId());
                    applyInfoService.updateStep(virtualNumber);
                }
                //查询是否有购房记录
                int value = houseDetailServiceImpl.countNum(hd.getRelatedParty());
                if (value > 0){
                    //判断是否为同一房源
                    HouseDetail h = houseDetailServiceImpl.countHouse(customerRegistration.getId());
                    Integer t = 0;
                    if (customerRegistration.getBlockCode().toString() != null){
                        if (h.getBlockCode() != customerRegistration.getBlockCode()){
                            t = 1;
                        }
                    }
                    if (customerRegistration.getUnitCode() != null){
                        if (!h.getUnitCode().equals(customerRegistration.getUnitCode())){
                            t = 1;
                        }
                    }
                    if (customerRegistration.getDoorplateCode() != null){
                        if (!h.getDoorplateCode().equals( customerRegistration.getDoorplateCode())){
                            t = 1;
                        }
                    }
                    //t为1的时候则是新改变的房源
                    if (t == 1){
                        //新改的房源是否被锁定
                        if (houseDetailServiceImpl.getBlockCodeY(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode()).getId() != null){
                            code = -10086;
                            regInfo.setInfoCode(code);
                            regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                            return JSON.toJSONString(regInfo);
                        }
                        //清除之前操作
                        houseDetailServiceImpl.clear(customerRegistration.getId());
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseType(hd)){
                            code = 1;
                        }
                    }else{
                        //修改最新信息
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseInfo(hd)){
                            code = 1;
                        }
                    }
                }else{
                    //修改最新信息
                    hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(),customerRegistration.getUnitCode(),customerRegistration.getDoorplateCode())));
                    if (houseDetailServiceImpl.updateHouseInfo(hd)){
                        code = 1;
                    }
                }
            }
        }else if (customerRegistration.getCustomerType().equals("已退房")){ //已退房入库
            customerRegistration.setPaymentType("");
            customerRegistration.setPaymentTypeMoney("0");
            customerRegistration.setSmallSum(0);
            customerRegistration.setSmallSumMoney("0");
            customerRegistration.setDownPayments(0);
            customerRegistration.setDownPaymentsMoney("0");
            customerRegistration.setIdentify(0);
            customerRegistration.setIdentifyMoney("0");
            customerRegistration.setLargeSum(0);
            customerRegistration.setLargeSumMoney("0");
            customerRegistration.setIdentify(0);
            customerRegistration.setIdentifyMoney("0");
            customerRegistration.setMoneyBack("0");
            //2019-12-25 下午  首付及更新最新的报备状态  如果是报备客户  Chenxinlele
            String customerSource = customerRegistrationServiceImpl.findBb(customerRegistration.getId());
            if (customerSource != null){ //执行佣金状态操作
                String virtualNumber = customerRegistrationServiceImpl.findvirtualNumber(customerRegistration.getId());
                applyInfoService.updateTStep(virtualNumber);
            }
            if (customerRegistrationServiceImpl.updateCustomerType(customerRegistration) > 0){
                //操作金额及客户信息
                Integer artificial = customerRegistrationServiceImpl.findBySaleType(customerRegistration.getId());   //赋值为最初的销控状态
                if (artificial == null || artificial == ' '){
                    artificial = 0;
                }
                if (houseDetailServiceImpl.updateHouseTypeN(customerRegistration.getId(),artificial)){
                    code = 1;
                }
            }
        }else if (customerRegistration.getCustomerType().equals("明确拒绝")){ //明确拒绝入库
            customerRegistration.setPaymentType("");
            customerRegistration.setPaymentTypeMoney("0");
            customerRegistration.setSmallSum(0);
            customerRegistration.setSmallSumMoney("0");
            customerRegistration.setDownPayments(0);
            customerRegistration.setDownPaymentsMoney("0");
            customerRegistration.setLargeSum(0);
            customerRegistration.setLargeSumMoney("0");
            customerRegistration.setIdentify(0);
            customerRegistration.setIdentifyMoney("0");
            customerRegistration.setMoneyBack("0");
            customerRegistration.setIdentify(0);
            customerRegistration.setIdentifyMoney("0");
            //2019-12-26 下午  首付及更新最新的报备状态  如果是报备客户  Chenxinlele
            String customerSource = customerRegistrationServiceImpl.findBb(customerRegistration.getId());
            if (customerSource != null){ //执行佣金状态操作
                String virtualNumber = customerRegistrationServiceImpl.findvirtualNumber(customerRegistration.getId());
                applyInfoService.updateJJStep(virtualNumber);
            }
            if (customerRegistrationServiceImpl.updateCustomerType(customerRegistration) > 0){
                Integer artificial = customerRegistrationServiceImpl.findBySaleType(customerRegistration.getId());   //赋值为最初的销控状态
                if (artificial == null || artificial == ' '){
                    artificial = 0;
                }
                if (houseDetailServiceImpl.updateHouseTypeN(customerRegistration.getId(),artificial)){
                    code = 1;
                }
            }
        }else if (customerRegistration.getCustomerType().equals("继续跟踪")){ //继续跟踪入库
            if (customerRegistrationServiceImpl.updateCustomerType(customerRegistration) > 0){
               /* HouseDetail hd = new HouseDetail();
                hd.setRelatedParty(customerRegistration.getId());
                hd.setRelatedAdviser(customerRegistration.getReceptionId());
                hd.setType("1");*/
                if (houseDetailServiceImpl.countNum(customerRegistration.getId()) < 1){
                    Integer artificial = customerRegistrationServiceImpl.findBySaleType(customerRegistration.getId());   //赋值为最初的销控状态
                    if (artificial == null || artificial == ' '){
                        artificial = 0;
                    }
                if (houseDetailServiceImpl.updateHouseTypeN(customerRegistration.getId(),artificial)){
                        code = 1;
                    }
                }
            }
        }else if (customerRegistration.getCustomerType().equals("认筹")){
            customerRegistration.setIdentify(1);
            customerRegistration.setMoneyBack(customerRegistration.getReturnedMoney());
            customerRegistration.setIdentifyMoney(customerRegistration.getReturnedMoney()); //金额（已回款）
            if (customerRegistrationServiceImpl.updateCustomerType(customerRegistration) > 0) {
                //清除认筹之外状态 20191203
                //客户表操作
                customerRegistrationServiceImpl.updateRc(customerRegistration.getId());
                HouseDetail hd = new HouseDetail();
                hd.setRelatedParty(customerRegistration.getId());
                hd.setRelatedAdviser(customerRegistration.getReceptionId());
                hd.setType("1");
                hd.setCjTime(df.format(new Date()));
                hd.setRelatedAdviser(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
                hd.setTotalActualAmount(customerRegistration.getTotalActualAmount());
                hd.setArtificial(1);
                //查询是否有购房记录
                int value = houseDetailServiceImpl.countNum(hd.getRelatedParty());
                if (value > 0) {
                    //判断是否为同一房源
                    HouseDetail h = houseDetailServiceImpl.countHouse(customerRegistration.getId());
                    Integer t = 0;
                    if (customerRegistration.getBlockCode().toString() != null) {
                        if (h.getBlockCode() != customerRegistration.getBlockCode()) {
                            t = 1;
                        }
                    }
                    if (customerRegistration.getUnitCode() != null) {
                        if (!h.getUnitCode().equals(customerRegistration.getUnitCode())) {
                            t = 1;
                        }
                    }
                    if (customerRegistration.getDoorplateCode() != null) {
                        if (!h.getDoorplateCode().equals(customerRegistration.getDoorplateCode())) {
                            t = 1;
                        }
                    }
                    //t为1的时候则是新改变的房源
                    if (t == 1) {
                        //新改的房源是否被锁定
                        if (houseDetailServiceImpl.getBlockCodeY(customerRegistration.getBlockCode(), customerRegistration.getUnitCode(), customerRegistration.getDoorplateCode()).getId() != null) {
                            code = -10086;
                            regInfo.setInfoCode(code);
                            regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                            return JSON.toJSONString(regInfo);
                        }
                        //清除之前操作
                        houseDetailServiceImpl.clear(customerRegistration.getId());
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(), customerRegistration.getUnitCode(), customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseType(hd)) {
                            code = 1;
                        }
                    } else {
                        //修改最新信息
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(), customerRegistration.getUnitCode(), customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseInfo(hd)) {
                            code = 1;
                        }
                    }
                } else {
                    //存在小订及认筹会有房源信息
                    if (customerRegistration.getBlockCode() != null && customerRegistration.getDoorplateCode() != null) {
                        //修改最新信息
                        hd.setId(Integer.valueOf(houseDetailServiceImpl.queryZsId(customerRegistration.getBlockCode(), customerRegistration.getUnitCode(), customerRegistration.getDoorplateCode())));
                        if (houseDetailServiceImpl.updateHouseInfo(hd)) {
                            code = 1;
                        }
                    }else{
                        code = 1;
                    }
                }
            }
        }
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    //导入答客百问
    @ResponseBody
    @RequestMapping(value = "/importQuestionsAnswersList",method = RequestMethod.POST)
    private String importQuestionsAnswersList(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) {
        if (uploadFile == null) {
            //判断文件大小
            return "上传文件为空文档";
        }
        Integer code = -1;
        BaseEntity baseEntity =new BaseEntity();
        questionsAnswersService.importQuestionsAnswersList(uploadFile,request);
        baseEntity.setInfoCode(code);
        baseEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return  JSON.toJSONString(baseEntity);
    }

    /**
     * 查询我要做小程序列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getHouseContactList",method = RequestMethod.GET)
    private String getHouseContactList(HttpServletRequest request) {
        List<Contact> lst = contactService.getHouseContactList();
        return JSON.toJSONString(lst);
    }


    /**
     * 验证码号
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/verificationPhone",method = RequestMethod.GET)
    private String verificationPhone(HttpServletRequest request){
        Integer code = 1;
        BaseEntity baseEntity =new BaseEntity();
        String phone = request.getParameter("phone");
        Integer uid = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId();
        //获取此手机号的信息
        CustomerRegistration cr = customerRegistrationService.findByPhone(phone);
        if (cr != null){
            //判断是否为同一置业顾问客户
            if (cr.getReceptionId() != uid){
                code = -4;
                baseEntity.setInfoCode(code);
                baseEntity.setInfoText("此号码用户已被"+cr.getReceptionName()+"录入！");
                return  JSON.toJSONString(baseEntity);
            }else{
                code = 4;
                baseEntity.setInfoCode(code);
                baseEntity.setInfoText("此号码用户不是首次来访！请在多次来访中操作。");
                return  JSON.toJSONString(baseEntity);
            }
        }else{
            code = 5;
            baseEntity.setInfoCode(code);
            baseEntity.setInfoText("验证通过");
        }
        return JSON.toJSONString(baseEntity);
    }

}
